package com.esinhai.tz.util;

import com.esinhai.tz.common.Const;
import com.esinhai.tz.common.ServerResponse;
import com.esinhai.tz.dao.UserTzMapper;
import com.esinhai.tz.pojo.UserTz;
import com.esinhai.tz.pojo.UserTzWithBLOBs;
import com.esinhai.tz.webservice.WebServiceFrontImages;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by 32988 on 2018/5/9.
 */
public class ExpireJobTask {

    @Autowired
    private UserTzMapper userTzMapper;

    public void doBiz() throws IOException {
        UserTz userTz = new UserTz();
        List<UserTzWithBLOBs> userTzList =  userTzMapper.selectUserTzBesyn();
        String status = "";
        UserTzWithBLOBs userTzWithBLOBs = new UserTzWithBLOBs();
        for (UserTzWithBLOBs user : userTzList) {com
            if ("0".equals(user.getBesyn())) {
                if (!StringUtil.isEmpty(user.getFace())) {
                    //通过，分割出图片的base64数据
                    String[] faces = user.getFace().split(Const.SIGN);
                    for (String face : faces) {
                        //把base64转成图片放在对应的路径下
                        if (face.contains("front:")) {
                            face = face.substring("front:".length());
                            status = WebServiceFrontImages.hskjWebService(user.getIdcard(), face);
                            if (status.equals("0")) {
                                updateByPrimaryKeySelective(userTzWithBLOBs,user.getObjectId(),"1");
                            } else if ("19".equals(status)) {
                                status = WebServiceFrontImages.hskjRemoveWebService(user.getIdcard());
                                updateByPrimaryKeySelective(userTzWithBLOBs,user.getObjectId(),status);
                            } else {
                                updateByPrimaryKeySelective(userTzWithBLOBs,user.getObjectId(),status);
                            }
                        }

                    }
                }
            }
            if ("19".equals(user.getBesyn())) {
                status = WebServiceFrontImages.hskjRemoveWebService(user.getIdcard());
                System.out.println("删除状态："+status);
                updateByPrimaryKeySelective(userTzWithBLOBs,user.getObjectId(),status);
            }
        }
    }
    private void updateByPrimaryKeySelective(UserTzWithBLOBs userTzWithBLOBs,String objectId , String besyn){
        userTzWithBLOBs.setBesyn(besyn);
        userTzWithBLOBs.setObjectId(objectId);
        userTzMapper.updateByPrimaryKeySelective(userTzWithBLOBs);
    }


}
